const util = require("../../utils/util.js");

Page({
  data: {

  },

  onLoad: function (options) {

    wx.getStorage({
      key: 'username',
      fail: err => {
        console.log(err)
      }
    })
  },

  formSubmit: function (e) {
    var username = e.detail.value.username;
    var password = e.detail.value.password;
    // 判断用户名是否为空
    if (username.length !== 0) {
      // 判断密码是否为空
      if (password.length === 0) {
        util.alertView("提示", "请输入密码", () => {});
      }else{
        wx.request({
          url: 'http://47.107.121.61/user/login',
          data: {
            username: username,
            password: password,
          },
          method: "POST",
          header: {
            'content-type': 'application/json'
          },
          success: res => {
            const status = res.data.status;
            const msg = res.data.msg;
            if (status == '200') {
              console.log(res)
              wx.setStorage({
                key: "username",
                data: username
              })
              util.alertView("提示", msg, function () {
                wx.switchTab({
                  url: '../user/user',
                })
              })
            } else if (status == '404') {
              util.alertView("提示", msg, () => { })
            } else {
              util.alertView("提示", "请求超时请重试", () => { })
            }
          },
          fail: err => {
            console.log(err)
          }
        })
      }
    }else {
      util.alertViewWithCancel("提示", "请输入用户名", () => { }, "true");
    }
  }
})